import {colorUISdk} from "../../config/mp-sdk";
import {updateCurrentPageShare} from "../../mp-sdk/lib/share";

const app = getApp();
let interstitialAd = null
Page({
    data: {
        scrollTop: 0,
        swiperList: [
            {img: "/static/img/banner1.png"},
            {img: "/static/img/banner2.png"}
        ]
    },
    onLoad(options) {
        //获取服务器图片
        this.getOriginImg();
        // 通过分享进入的用户
        const referrer_id = options.referrer_id;

        if (referrer_id) {
            //增加分享记录
            colorUISdk.request({
                url: 'api/user/set_scan_record',
                method: 'POST',
                data: {
                    qrcode: null,
                    type: 4,
                    pid: referrer_id
                }
            })
        }

        //通过代理码进入
        let agent_id = null;

        // scene 是经过 URL 编码的，需要手动解析
        if (options.scene) {
            const sceneParams = decodeURIComponent(options.scene);
            // 转换成对象
            const params = {};
            sceneParams.split('&').forEach(kv => {
                const [key, value] = kv.split('=');
                params[key] = value;
            });
            agent_id = params.agent_id;
        }

        if (agent_id) {
            //增加分享记录
            colorUISdk.request({
                url: 'api/user/set_scan_record',
                method: 'POST',
                data: {
                    qrcode: null,
                    type: 3,
                    pid: agent_id
                }
            })
        }

    },
    getOriginImg() {
        colorUISdk.request({
            url: 'api/tutorial',
            method: 'GET',
            data: {from: this.data.modalBox}
        }).then(res => {
            if (res.code === 1) {
                // 登录成功后立即更新分享配置
                updateCurrentPageShare();
            } else {
                wx.showToast({title: res.msg, icon: 'none'});
            }
        }).catch(() => {
            wx.showToast({title: '系统异常', icon: 'none'});
        });


    },
    previewImage(e) {
        const current = e.currentTarget.dataset.url;
        const urls = this.data.shop.map(item => item.imgUrl);
        console.log(current)
        wx.previewImage({
            current,
            urls
        });
    },
    getColor() {
        let colorArr = ['yellow', 'orange', 'red', 'pink', 'mauve', 'purple', 'blue', 'cyan', 'green', 'olive', 'grey', 'brown'];
        let a = colorArr[Math.floor(Math.random() * colorArr.length)]
        console.log(a)
    },
    goToBuy() {
        wx.navigateTo({
            url: '/pages/yd_product/home'
        })
    },
    goToGame() {
        wx.navigateToMiniProgram({
            appId: 'wx283e5368c3a60171',
            path: 'page/index/index?id=123',
            extraData: {
                foo: 'bar'
            },
            envVersion: 'release',
            success(res) {
                // 打开成功
            }
        })
    },
    onShow() {

    },
    // 监听用户滑动页面事件。
    onPageScroll(e) {
        this.setData({scrollTop: e.scrollTop})
    }
})
